import copy
from base import XMLBase, Reference, LinkType
from characterization import Representation

class Algorithm(XMLBase):
    _node_name = 'Algorithm'
    _attrib_map = {'Name': 'name',
                   'Description': 'description'}
    _list_map = {'Reference':{'attribute':'references',
                              'class':Reference}}

    def __init__(self, name=None, description=None,
                 references=[]):
        self.name = name
        self.description = description
        self.references = copy.copy(references)

class Parameter(XMLBase):
    _node_name = 'Parameter'
    _attrib_map = {'Name': 'name',
                   'Description': 'description',
                   'Datatype':'datatype',
                   'DefaultValue':'default_value',
                   'Cardinality':'cardinality',
                   'Units':'units',
                   'UCD':'ucd'}
    _list_map = {'Reference':{'attribute':'references',
                              'class':Reference}}

    def __init__(self, name=None, description=None, datatype='str',
                 default_value=None, cardinality=None, 
                 units=None, ucd=None, references=[]):

        self.name = name
        self.description = description
        self.datatype = datatype
        self.default_value = default_value
        self.cardinality = cardinality
        self.units = units
        self.ucd = ucd
        self.references = copy.copy(references)

    def read(self, xml_str):
        XMLBase.read(self, xml_str)
        if self.default_value and self.datatype:
            val = self.default_value
            self.default_value = eval('%s(val)' % (self.datatype))

class ProgramLink(LinkType):
    _node_name = 'Program'

class Program(XMLBase):
    _node_name = 'Program'
    _attrib_map = {'Name': 'name',
                   'Description': 'description',
                   'Author':'author',
                   'URL':'url'}
    _list_map = {'Reference':{'attribute':'references',
                              'class':Reference},
                 'Algorithm':{'attribute':'algorithms',
                              'class':Algorithm},
                 'Representation':{'attribute':'representations',
                                   'class':Representation},
                 'Parameter':{'attribute':'parameters',
                              'class':Parameter}}

    def __init__(self, name=None, description=None, 
                 author=None, url=None, algorithms=[],
                 representations=[], parameters=[],
                 references=[]):

        self.name = name
        self.description = description
        self.author = author
        self.url = url
        self.algorithms = copy.copy(algorithms)
        self.representations = copy.copy(representations)
        self.parameters = copy.copy(parameters)
        self.references = copy.copy(references)
